package com.paradogs.gs.modules.inventory;

import com.google.common.io.FileWriteMode;
import com.paradogs.gs.misc.Bow;
import com.paradogs.gs.misc.DBMultiRowManager;
import com.paradogs.gs.misc.annotations.GameManager;
import com.paradogs.gs.db.Material;
import com.paradogs.gs.modules.inventory.dao.MaterialDao;
import com.paradogs.gs.modules.inventory.framework.ItemGainProxy;
import com.paradogs.gs.modules.inventory.framework.ItemType;

import java.util.Map;

/**
 * @author: yumi
 * @date: 2024/10/29  16:02
 * @Description: TODO
 */
@GameManager
public class MaterialMgr extends DBMultiRowManager<MaterialDao, Material> {
    @Override
    public void onInitialization(Map<Long, Material> data) {

    }

    @ItemGainProxy(ItemType.MATERIAL)
    public void itemProxy(int itemId, int count) {
        Long id = Long.valueOf(itemId);
        Map<Long, Material> dataMap = __raw__();
        if (dataMap.containsKey(Long.valueOf(id))) {
            Material material = dataMap.get(id);
            update(material.getId(), Material::setCount, material.getCount() + count);
        } else {
            // 新增
            __update__(new Material(itemId, count));
        }
    }

    /** 扣减道具 */
    public void consume(Map<Integer, Integer> consume) {
        // 检查是否满足
        for (Map.Entry<Integer, Integer> e : consume.entrySet()) {
            Integer itemId = e.getKey();
            Integer count = e.getValue();
            Material data = __raw__().get(Long.valueOf(itemId));
            Bow.asserts(data != null && data.getCount() >= count, "道具不足");
        }
        // 扣减
        for (Map.Entry<Integer, Integer> e : consume.entrySet()) {
            Integer itemId = e.getKey();
            Integer count = e.getValue();
            Material data = __raw__().get(Long.valueOf(itemId));
            data.setCount(data.getCount() - count);
        }
    }
}
